﻿#include "ThreadManger.h"
#include "WorkThread.h"
#include <QJsonObject>

QList<WorkThread *> ThreadManger::listThread;

ThreadManger::ThreadManger(QObject *parent)
	: QObject(parent)
{

}

ThreadManger::~ThreadManger()
{

}

void ThreadManger::initThreadManger(int count)
{
	qDeleteAll(listThread);
	listThread.clear();
	WorkThread * pThread = NULL;
	for (int i = 0; i < count; ++i)
	{
		pThread = new WorkThread();
		listThread.append(pThread);
		pThread->start();
	}
}

WorkThread * ThreadManger::getMinPayThread()
{
	int iMinPay = 0;
	int iRef = 0;

	WorkThread * pThread = NULL;
	for (int i = 0; i < listThread.count(); ++i)
	{
		WorkThread * curThread = listThread.at(i);

		iRef = curThread->getRefCount();
		if (iRef == 0 || iRef < iMinPay)
		{
			iMinPay = iRef;
			pThread = curThread;
		}
	}
	return pThread;
}